/****************************************************
	文件：ResCache.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2021/1/2 16:47:23
	功能：资源缓存
*****************************************************/

using System;
using System.Collections.Generic;
using HTFW.Logger;
using HTFW.Utility.Global;
using IDisposable = HTFW.Core.IDisposable;

namespace HTFW.Res
{
    public class ResCache : Singleton<ResCache>, IResCache, IDisposable
    {
        /// <summary>
        /// 资源缓存（key：Path/ABName）
        /// </summary>
        private Dictionary<string, ResData> mResDict;

        public void Init()
        {
            mResDict = new Dictionary<string, ResData>();
        }

        public void Dispose()
        {
            mResDict = null;
        }

        public void Add(string key, ResData resData)
        {
            if (mResDict.ContainsKey(key))
            {
                HLog.Error($"ResCache add error! 重复缓存资源：{key}");
                return;
            }

            mResDict.Add(key, resData);
        }

        public bool TryGet(string key, out ResData res)
        {
            if (mResDict.TryGetValue(key, out res))
            {
                return true;
            }

            return false;
        }

        public ResData Get(string key)
        {
            if (mResDict.TryGetValue(key, out var res))
            {
                return res;
            }

            //HLog.Warning($"ResCache Get error! 缓存中不存在资源：{key}");
            return null;
        }

        public ResData Get(uint crc)
        {
            return null;
        }

        public bool Remove(string key)
        {
            if (mResDict.ContainsKey(key))
            {
                mResDict.Remove(key);
                return true;
            }

            HLog.Warning($"ResCache Remove error! 缓存中不存在资源：{key}");
            return false;
        }

        public bool Remove(uint crc)
        {
            return false;
        }

        public bool Contains(string key)
        {
            return mResDict.ContainsKey(key);
        }

        public bool Contains(ResData resData)
        {
            return mResDict.ContainsValue(resData);
        }
    }
}